﻿
/*********************************************************************************
--时间：2012-01-14 00:09:14
--作者：jjpeng
--邮箱：ling121211@gmail.com
--说明：缓存管理帮助类
--
*********************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Web;
using System.Web.Caching;

namespace BigData.Server.NewsUtil
{
    /// <summary>
    /// 缓存处理类
    /// </summary>
    public class CacheHelper
    {

        /// <summary>
        /// 读取缓存
        /// </summary>
        /// <param name="CacheKey">key名</param>
        /// <returns>缓存对象</returns>
        public static object GetCache(string cacheKey)
        {
            return HttpRuntime.Cache[cacheKey];
        }

        /// <summary>
        /// 根据键前缀获取对应的值链表
        /// </summary>
        /// <param name="keyPrefix"></param>
        /// <returns></returns>
        public static List<object> GetCacheModelsByPrefixKey(string keyPrefix)
        {
            List<object> retList = new List<object>();
            foreach (DictionaryEntry cache in HttpRuntime.Cache)
            {
                string cacheKey = cache.Key.ToString();

                if (cacheKey.StartsWith(keyPrefix))
                {
                    retList.Add(GetCache(cacheKey));
                }
            }
            return retList;
        }

        /// <summary>
        /// 读取缓存-若空则设置缓存值
        /// </summary>
        /// <param name="cacheKey">缓存ID</param>
        /// <param name="expire">过期时间</param>
        /// <param name="action">当缓存为空时的动作</param>
        /// <returns></returns>
        public static object GetCache(string cacheKey, DateTime expire, Func<object> action)
        {
            object o = GetCache(cacheKey);
            if (o == null)
            {
                o = action.Invoke();
                SetCache(cacheKey, o, expire);
            }
            return o;
        }

        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <param name="cacheKey">key名</param>
        /// <param name="objObject">要被缓存的对象</param>
        public static void SetCache(string cacheKey, object objObject)
        {
            if (objObject != null)
            {
                HttpRuntime.Cache.Insert(cacheKey, objObject);
            }
            else
            {
                RemoveCache(cacheKey);
            }
        }

        /// <summary>
        /// 设置绝对到期缓存
        /// </summary>
        /// <param name="cacheKey">key名</param>
        /// <param name="objObject">要被缓存的对象</param>
        /// <param name="expire">过期时间</param>
        public static void SetCache(string cacheKey, object objObject, DateTime expire)
        {
            if (objObject != null)
            {
                HttpRuntime.Cache.Insert(cacheKey, objObject, null, expire, Cache.NoSlidingExpiration);
            }
            else
            {
                RemoveCache(cacheKey);
            }
        }

        /// <summary>
        /// 设置滑动过期缓存
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <param name="cacheValue"></param>
        /// <param name="slidingExpireTime"></param>
        public static void SetCache(string cacheKey, object cacheValue, TimeSpan slidingExpireTime)
        {
            if (cacheValue != null)
            {
                HttpRuntime.Cache.Insert(cacheKey, cacheValue, null, Cache.NoAbsoluteExpiration, slidingExpireTime);
            }
            else
            {
                RemoveCache(cacheKey);
            }
        }

        /// <summary>
        /// 设置绝对过期缓存
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <param name="cacheValue"></param>
        /// <param name="expire"></param>
        /// <param name="beforeRemoveCallback">缓存移除前的操作</param>
        public static void SetCache(string cacheKey, object cacheValue, DateTime expire, CacheItemRemovedCallback beforeRemoveCallback)
        {
            if (cacheValue != null)
            {
                HttpRuntime.Cache.Insert(cacheKey, cacheValue, null, expire, Cache.NoSlidingExpiration, CacheItemPriority.Default, beforeRemoveCallback);
            }
            else
            {
                RemoveCache(cacheKey);
            }
        }

        /// <summary>
        /// 根据键名清除缓存
        /// </summary>
        /// <param name="keys">key名</param>
        public static void RemoveCache(string cacheKey)
        {
            HttpRuntime.Cache.Remove(cacheKey);
        }

        /// <summary>
        /// 从缓存中去除前缀缓存名为参数的缓存
        /// </summary>
        /// <param name="likeCacheID"></param>
        public static void RemoveCacheByPrefix(string prefixCacheKey)
        {
            foreach (DictionaryEntry cache in HttpRuntime.Cache)
            {
                string cacheKey = cache.Key.ToString();

                if (cacheKey.StartsWith(prefixCacheKey))
                {
                    RemoveCache(cacheKey);
                }
            }
        }

    }
}
